import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import AppLayout from '@/layout/AppLayout.vue'
import { clearPending } from '@/utils/request' // 引入在axios暴露出的clearPending函数
import productRoute from './modules/product'
import NProgress from '@/config/nprogress'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    component: AppLayout,
    children: [
      {
        path: '',
        name: 'home',
        component: () => import('../views/home/index.vue'),
        meta: { title: '首页' },
      },
      productRoute,
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
  },
]

// console.log('router ->', routes)

const router = createRouter({
  history: createWebHashHistory(), // 路由模式
  routes: routes, // 路由规则
})

router.beforeEach((to, from, next) => {
  NProgress.start() // 开始加载进度条
  // 在跳转路由之前，先清除所有的请求
  clearPending()
  // ...
  next()
})

router.afterEach(() => {
  NProgress.done() // 结束进度条
})

export default router
